package com.ztc.ZtcRefundRatio.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aqgj.util.JsonWrapper;
import com.ztc.ZtcRefundRatio.bean.ZtcRefundRatio;
import com.ztc.ZtcRefundRatio.bean.ZtcRefundRatioInfo;
import com.ztc.ZtcRefundRatio.dao.ZtcRefundRatioDao;
import com.ztc.ZtcRefundRatio.dao.ZtcRefundRatioInfoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;


@Service("ZtcRefundRatioInfo")
public class ZtcRefundRatioInfoManager {

    @Autowired
    ZtcRefundRatioInfoDao ztcRefundRatioInfoDao;
    @Autowired
    ZtcRefundRatioDao ztcRefundRatioDao;

    /**
     * 保存扣费比例设置.
     * @param ztcRefundRatioInfo 扣费比例设置信息
     * @param ztcRefundRatio  扣费比例详情json字符串
     * @return
     */
    @Transactional
    public HashMap<String,Object> saveInfo(ZtcRefundRatioInfo ztcRefundRatioInfo, String ztcRefundRatio){
        String id=null;
        if (ztcRefundRatioInfo.getId() != null &&!"".equals(ztcRefundRatioInfo.getId())) {
            id = ztcRefundRatioInfoDao.update(ztcRefundRatioInfo);
        } else {
            id = ztcRefundRatioInfoDao.save(ztcRefundRatioInfo);
        }
        if (id != null) {
            JSONArray jsonArray = JSON.parseArray(ztcRefundRatio);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                ZtcRefundRatio ztcRefundRatio1 = new ZtcRefundRatio();
                ztcRefundRatio1.setZtcRefundRatioInfoId(id);
                String ifRefund = jsonObject.get("isRefund").toString();
                //是否允许退票（1允许，2不允许）
                if ("1".equals(ifRefund)) {
                    ztcRefundRatio1.setChargeScale(new BigDecimal(jsonObject.get("chargeScale").toString()));
                }
                ztcRefundRatio1.setStartTime(Integer.valueOf(jsonObject.get("startTime").toString()));
                ztcRefundRatio1.setIsRefund(ifRefund);
                ztcRefundRatio1.setDisabled(jsonObject.get("disabled").toString());
                ztcRefundRatio1.setEndTime(Integer.valueOf(jsonObject.get("endTime").toString()));
                ztcRefundRatioDao.save(ztcRefundRatio1);
            }
            return JsonWrapper.successWrapper("设置操作成功！");
        }
        return JsonWrapper.failureWrapperMsg("设置操作失败！");
    }


    public void removeById(String carId) {
        ztcRefundRatioInfoDao.removeById(carId);
    }

    public ZtcRefundRatioInfo findUniqueBy(String propertyName, Object value) {
        return ztcRefundRatioInfoDao.findUniqueBy(propertyName, value);
    }

    public List<ZtcRefundRatioInfo> findBy(String propertyName1, Object value1, String propertyName2, Object value2) {
        return ztcRefundRatioInfoDao.findBy(propertyName1, value1, propertyName2, value2);
    }

    public List<ZtcRefundRatioInfo> findBy(String propertyName, Object value) {
        return ztcRefundRatioInfoDao.findBy(propertyName, value);
    }

    public ZtcRefundRatioInfo get(String zid) {
        return ztcRefundRatioInfoDao.get(zid);
    }

    public  String save(ZtcRefundRatioInfo ztcRefundRatioInfo){
        return ztcRefundRatioInfoDao.save(ztcRefundRatioInfo);
    }
}
